/*
  Tên:Nguyễn Văn Huy
  MSSV:11374891
  Lớp:DHTH7DLT
  Ngày hoàn thành:28/04/2012
  */
#ifndef TASKSMODEL_H
#define TASKSMODEL_H
#include <QAbstractItemModel>
#include <QSqlDatabase>
#include <QSqlQuery>
#include "mysqlconnection.h"
class TasksModel : public QAbstractItemModel
{
     Q_OBJECT
public:
   explicit TasksModel(QObject *parent = 0);
    virtual QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const;
     virtual Qt::ItemFlags flags ( const QModelIndex & index ) const;
        virtual int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
        virtual QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
        virtual int rowCount (const QModelIndex & parent = QModelIndex() ) const;
        virtual bool hasChildren ( const QModelIndex & parent = QModelIndex() ) const;
        virtual void sort ( int column, Qt::SortOrder order = Qt::AscendingOrder );
        virtual QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const;
        virtual QModelIndex parent ( const QModelIndex & index ) const;
        virtual bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
        virtual bool setHeaderData ( int section, Qt::Orientation orientation, const QVariant & value, int role = Qt::EditRole );
        int selectedId;
    QModelIndex indexForId(int id);

private:
    int nrOfColumns;
    MySqlConnection *DBC;
    mutable QMap<qint64, QSqlQuery*> subQueries;
        mutable QMap<qint64, int> rowsCount;
        mutable QSqlQuery topQuery;
        mutable int topRowsCount;
        mutable int topRowsFetched;
        mutable bool topQueryReady;
        QSqlQuery* verifyAndPrepareQuery (const QModelIndex& index) const;
        int totalCount(const qint64 id, bool force=false) const;
        void recountTotalCount(const qint64 id) const;
        QStringList headerNames;
        struct NodeParams {
            int row;
            int parentId;
        };
        mutable QMap<qint64, NodeParams> nodeParams;
signals:

public slots:
};

#endif // TASKSMODEL_H
